
$(function() {
	Tip.emp('负责人');
	
	$("#machine").autocomplete(Test.machine);
	
	$('#customer').change(function() {
		var a = $(this).val();
		// Pay.supplier(a);
		
		var body = { };
		body.id = a;
		$.getJSON('contact.php?do=Customer.ajax_id', body, function(json) {
			$("#a_name").val(json.name);
			$("#a_fullname").val(json.fullname);
			$("#a_address").val(json.address);
			$("#a_web").val(json.url);
			
			/*
			var s = $("#name").val();
			if (s == null || s == '') {
				$("#name").val(json.code);
			}*/
			
			// $("#emp").val(json.emp);
		});
		
		var body = {};
		body.q = a;
		
		$.getJSON('contact.php?do=Customer.ajax_linkman', body, function(json) {	
			// alert($.toJSON(json));
			Common.linkman(json);
		});
	});
	
	$("#label_linkman_a").hide();
	
	Tip.supplier();
	
	$('#supplier').blur(function() {
		var a = $(this).val();
		
		var body = { };
		body.id = a;
		$.getJSON('contact.php?do=Supplier.ajax_id', body, function(json) {
			$("#c_name").val(json.name);
			$("#c_fullname").val(json.fullname);
			$("#c_address").val(json.address);
			$("#c_web").val(json.url);
		});
		
		var body = {};
		body.q = a;
		
		$.getJSON('finance.php?do=PaymentMy.ajax_linkman', body, function(json) {			
			Supplier.linkman(json);
		});
	});
	
	$("#label_linkman").hide();
	
	$('#date').datepicker(_date1_option);
	$('#expect').datepicker(_date1_option);
	
	_change("#address", "select[id^='address_t']:visible");
	
	_change("input[id='type']:visible", "select[id^='type_t']:visible");
	
	_currency($('#currency').val());
	
	$('#currency').change(function() {
		var s = $(this).val();
		_currency(s);
	});
	
	$('#amount').change(function() {
		var a = $(this).val();
		var b = $("#currency").find("option:selected").text();
		Currency.upper(a, b, "#label_amount");
	});
	
	$('textarea').css("width", 400);
	
	var options = {
			editable: true,
		    enableAddRow: true,
		    enableCellNavigation: true,
		    asyncEditorLoading: false,
		    rowHeight: 30
		};
	
	var grid = new Slick.Grid($("#myGrid"), data, columns, options);
	
	grid.onAddNewRow.subscribe(function (e, args) {
		// alert("onaddnewrow");
		
        var item = {unit:"PCS", quantity:"1", price:"1.00", amount:"1.00", ok: true};
        item.date = current_date();
        
    	item.amount = fxmul(item.quantity, item.price);
    	
    	// alert($.toJSON(item));
    	
        $.extend(item, args.item);
        data.push(item);
        grid.invalidateRows([data.length - 1]);
        grid.updateRowCount();
        grid.render();
        
        total();
    });
	
	grid.onCellChange.subscribe(function(e, args) {
		item = args.item;
		currentCell = args.cell;
		currentRow = args.row;
		
    	item.amount = fxmul(item.quantity, item.price);
    	// alert($.toJSON(item));
    	
    	grid.updateRow(currentRow);
    	grid.updateRowCount();
        grid.render();
		
    	total();
    });
	
});

function total() {
	var total = 0;
	$.each(data, function(k, v) {
		if (v.ok) {
			total += parseFloat(v.amount);
		}		
	});
		
	$("#amount_t").html(total);
	$("#amount").val(total);
	
	var b = $("#currency").find("option:selected").text();
	Currency.upper(total, b, "#label_amount");
}

function _currency(s) {
	if (s == null || s == '-')　{		
		return false;
	}
	
	var body = { };
	body.currency = s;
	$.post('finance.php?do=Finance.ajax_exchange', body, function(s) {
		exchange = s;
		$('#rate').val(s);
		
		_update();
	});
}

function _update() {
	var a = $("#amount").val();
	var b = $("#currency").find("option:selected").text();

	Currency.upper(a, b, "#label_amount");
}
